package com.wxpay.service.impl;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.wxpay.entity.Order;
import com.wxpay.service.BaseService;
import com.wxpay.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @Author worlt
 * @Date 2025/4/21 下午11:20
 */
@Service
public class OrderServiceImpl extends BaseService<Order> implements OrderService {

    private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Override
    public Order add(String body, String money) {
        Order order = null;
        try {
            order = new Order();
            order.setOrderNo(System.currentTimeMillis() + "");
            order.setBody(body);
            order.setMoney(money);
            order.setStatus(0);
            order.setAddTime(DateUtil.now());
            
            Integer i = super.save(order);
            
            if (i == null || i <= 0) {
                logger.error("订单保存失败 - 商品:{}, 金额:{}", body, money);
                throw new Exception("订单保存失败");
            }
            
            logger.info("订单创建成功 - 订单号:{}, 金额:{}", order.getOrderNo(), money);
        } catch (Exception e) {
            logger.error("订单创建异常 - 商品:{}, 金额:{}, 异常:{}", body, money, e.getMessage());
            e.printStackTrace();
        }
        return order;
    }

    @Override
    public Order getOrderInfo(String orderNo) {
        Order order = null;
        try {
            Order orderWhere = new Order();
            orderWhere.setOrderNo(orderNo);
            order = super.selectOne(orderWhere);
            
            if (order == null) {
                logger.warn("查询订单不存在 - 订单号:{}", orderNo);
            }
        } catch (Exception e) {
            logger.error("查询订单异常 - 订单号:{}, 异常:{}", orderNo, e.getMessage());
            e.printStackTrace();
            return null;
        }
        return order;
    }

    @Override
    public boolean paySuccess(String orderNo, String payNo, String payTime) {
        logger.info("处理支付回调 - 订单号:{}, 支付号:{}", orderNo, payNo);
        
        try {
            // 查询订单
            Order order = getOrderInfo(orderNo);
            
            if (order == null) {
                logger.error("支付回调处理失败 - 订单不存在，订单号:{}", orderNo);
                throw new Exception("订单不存在");
            }
            
            // 检查订单状态
            if (order.getStatus() != null && order.getStatus().intValue() == 1) {
                logger.info("订单已支付，无需重复处理 - 订单号:{}", orderNo);
                return true;
            }

            // 更新订单状态
            Order orderData = new Order();
            orderData.setStatus(1);
            orderData.setPayNo(payNo);
            orderData.setPayTime(payTime);

            Example example = new Example(Order.class);
            Criteria criteria = example.createCriteria();
            criteria.andEqualTo("orderNo", orderNo);
            
            // 执行更新
            int updateCount = super.updateSelective(orderData, example);
            
            if (updateCount > 0) {
                logger.info("订单支付成功 - 订单号:{}, 支付号:{}", orderNo, payNo);
                return true;
            } else {
                logger.error("订单状态更新失败 - 订单号:{}", orderNo);
                return false;
            }
        } catch (Exception e) {
            logger.error("订单支付处理异常 - 订单号:{}, 异常:{}", orderNo, e.getMessage());
            e.printStackTrace();
        }
        return false;
    }
}
